package demo12;

import demo11.TopNJob;
import demo5.DescIntWritable;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.awt.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class ReduceJoinJob {

    public static void main(String[] args) throws Exception{
// 1. 初始化配置
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop10:9000");
        //2. 创建job
        Job job = Job.getInstance(conf);
        job.setJarByClass(ReduceJoinJob.class);

        //3. 设置输入格式化工具和输出格式化
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        //4. 设置输入路径和输出路径
        TextInputFormat.addInputPath(job, new Path("/mapreduce/demo12"));
        TextOutputFormat.setOutputPath(job, new Path("/mapreduce/demo12/out"));

//        TextInputFormat.addInputPath(job, new Path(args[0]));
//        TextOutputFormat.setOutputPath(job, new Path(args[1]));
        //5. 设置mapper和reducer
        job.setMapperClass(ReduceJoinMapper.class);
        job.setReducerClass(ReduceJoinReducer.class);

        // 6. 设置mapper的kv类型和reducer的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(TableBean.class);

        job.setOutputKeyClass(TableBean.class);
        job.setOutputValueClass(NullWritable.class);

        // 7. 启动job
        boolean result = job.waitForCompletion(true);
        System.out.println(result);
    }

    static class ReduceJoinMapper extends Mapper<LongWritable, Text,Text,TableBean>{
        String fileName;

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            FileSplit fileSplit = (FileSplit) context.getInputSplit(); // 默认拿到逻辑切片
            fileName = fileSplit.getPath().getName(); // 获取当前mapTask读取split逻辑切片对应的文件名
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] arr = value.toString().split("\t");
            TableBean tableBean = new TableBean();
            if (fileName.contains("order")){ // 判断读取的文件是订单还是商品
                tableBean.setId(arr[0]); // 订单编号
                tableBean.setPid(arr[1]); // 商品编号
                tableBean.setAmount(Integer.parseInt(arr[2])); // 购买数量
                tableBean.setFlag("order"); // 标签
                context.write(new Text(arr[1]),tableBean); // 商品编号为key tableBean对象作为 value
            }else {
                tableBean.setPid(arr[0]); // 商品编号
                tableBean.setPname(arr[1]); // 商品名称
                tableBean.setFlag("pd"); // 标签
                context.write(new Text(arr[0]),tableBean); // 商品编号为key tableBean对象作为 value
            }
        }
    }

    static class ReduceJoinReducer extends Reducer<Text,TableBean,TableBean, NullWritable>{
        @Override
        protected void reduce(Text key, Iterable<TableBean> values, Context context) throws IOException, InterruptedException {
            /*
            * key     value
            * 商品编号  集合
            *
            * */
            String pname = ""; // 存储商品名称
            List<TableBean> list = new ArrayList<>(); // 存储和订单有关系的tableBean
            for (TableBean tableBean : values) {
                if (tableBean.getFlag().equals("order")){
                    TableBean tableBean1 = new TableBean();
                    try {
                        BeanUtils.copyProperties(tableBean1,tableBean);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else {
                    pname = tableBean.getPname();
                }
            }

            // 遍历list集合，循环读取订单对象，并且将商品名称赋值给订单 对象中的商品名称属性
            for (TableBean tableBean : list) {
                tableBean.setPname(pname);
                context.write(tableBean,NullWritable.get());
            }
        }
    }
}
